package com.nwpu.coursesupport.service.imple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.SubmitTaskStatus;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.ServiceUtil;
import com.nwpu.coursesupport.view.TaskDetailStuView;
import com.nwpu.coursesupport.view.TaskPersonalDetailAssistantView;
import com.nwpu.coursesupport.view.criteria.TaskDetailStuViewSearchCriteria;
import com.nwpu.coursesupport.view.criteria.TaskPersonalDetailAssistantViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.TaskDetailStuViewMapper;
import com.nwpu.coursesupport.view.mapper.TaskPersonalDetailAssistantViewMapper;

@Service
public class TaskPersonalDetailAssistantViewService extends AbstractService{
	
	@Autowired
	private TaskDetailStuViewMapper taskDetailStuViewMapper;
	//助教个人任务与学生个人任务底层实现一样，所以这里调用的接口和实现的方式与student一样

    protected static Logger logger = LoggerFactory.getLogger(TaskPersonalDetailAssistantViewService.class);
    
    public JSONResult detail(IDParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		int id =(Integer)getSession().getAttribute("id");
		TaskDetailStuViewSearchCriteria sc=new TaskDetailStuViewSearchCriteria();
		sc.setAssignId(p.getId());
		sc.setStuId(id);
		sc.setType(TaskType.PERSONALTASK);
		
		TaskDetailStuView view =this.taskDetailStuViewMapper.taskPersonalDetail(sc);
		if(view==null){
			logger.error("该任务不存在");
			return rst.fail("该任务不存在");
		}
		//如果已过任务截止时间或者已经被批改
		if(view.getSubmitResult().getStatus()==SubmitTaskStatus.HAVEREAD){
			view.setCanntSubmit(Status.YES);
		}else{//既未过期又未被批改
			view.setCanntSubmit(Status.NO);
		}
		view.setAttachment(ServiceUtil.DeserializeFiles(view.getAttachmentBytes()));
		if(view.getSubmitResult().getIsAnswerShow()==Status.YES){
			view.setAnswerAttachment(ServiceUtil.DeserializeFiles(view.getAnswerAttachmentBytes()));
		}
		if(view.getSubmitResult().getStatus().ordinal()>=SubmitTaskStatus.COMMITTED.ordinal()){
			view.getSubmitResult().setResultAttachment(ServiceUtil.DeserializeFiles(view.getSubmitResult().getResultAttachmentBytes()));
		}
		
		rst.addData("view", view);
		rst.success();
		return rst;
	}
}
